{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.4 Teleportation\n",
    "\n",
    "* [Q# exercise: Teleportation](./1-Basic_Quantum_Concepts/4-Teleportation.ipynb#qtransformation)\n",
    "* [Q# exercise: Another Teleportation Exercise](./1-Basic_Quantum_Concepts/4-Teleportation.ipynb#qtransformation2)\n",
    "\n",
    "You have now learned all the tools you need to conduct teleportation! Not on yourself for now, but on\n",
    "qubits. Like superdense coding, teleportation also utilizes the innate correlation between entangled pairs.\n",
    "While superdense coding sends one qubit to transmit two classical bits of information, teleportation uses\n",
    "two classical bits of information to pass along a qubit. When one qubit in the pair is manipulated, the\n",
    "other qubit indefinitely far away can somehow \"feel\" this manipulation. Doing measurement on one qubit\n",
    "tells us about the other qubit. Therefore, information can be transmitted. The word \"teleportation\"\n",
    "indicates information about a qubit is instantaneously transferred from one place to another without\n",
    "physically exchanging the qubit.\n",
    "\n",
    "![Figure 1.4 Teleportation](media/teleportation.png)\n",
    "\n",
    "Here is the circuit diagram to construct a teleportation:\n",
    "\n",
    "![Figure 1.4.1 Teleportation circuit](media/teleportation_circuit.png)\n",
    "\n",
    "Figure 1.4.1 Circuit diagram of teleportation.\n",
    "\n",
    "At the beginning of the circuit, Alice and Bob prepare an entangled pair of states $|A\\rangle$ and $|B\\rangle$ using the H\n",
    "and CNOT gates. They are then separated. If Alice wishes to send let Bob know about her other state $|A^{'}\\rangle$,\n",
    "she can use the state $|A\\rangle$ in her possession. After a series of operations on both sides as defined by the\n",
    "circuit, Bob obtains $|A^{'}\\rangle$.\n",
    "\n",
    "Let's take it apart.\n",
    "\n",
    "Say the pre-prepared Bell state after Alice and Bob entangle their qubits is $|\\psi^{+}\\rangle=\\frac{|00\\rangle+|11\\rangle}{\\sqrt{2}}$. They\n",
    "\n",
    "each take half of the pair with them and go separate ways. Alice has the first qubit and Bob has the second\n",
    "qubit.\n",
    "\n",
    "![Figure 1.4.2 Teleportation circuit](media/teleportation_circuit1.png)\n",
    "\n",
    "Figure 1.4.2 Alice and Bob entangle their states to each obtain a Bell state.\n",
    "\n",
    "Alice then wishes to tell Bob about $|A^{'}\\rangle$ but she cannot physically give it to Bob. It is an unknown state and\n",
    "can take the form $\\alpha|0\\rangle+\\beta|1\\rangle$, $\\alpha|1\\rangle+\\beta|0\\rangle$, $\\alpha|0\\rangle-\\beta|1\\rangle$ or $\\alpha|1\\rangle-\\beta|0\\rangle$. What she can do is to entangle\n",
    "her qubit in the Bell state with $\\alpha|0\\rangle+\\beta|1\\rangle$. Say $|A^{'}\\rangle=\\alpha|0\\rangle+\\beta|1\\rangle$ as an example, without loss of generality. First,\n",
    "Alice applies a CNOT with control $|A^{'}\\rangle$ and her qubit in $|\\psi^{+}\\rangle$. The input is $|A^{'}\\rangle|\\psi^{+}\\rangle$:\n",
    "\n",
    "$|A^{'}\\rangle|\\psi^{+}\\rangle=\\alpha|0\\rangle+\\beta|1\\rangle\\frac{|00\\rangle+|11\\rangle}{\\sqrt{2}}$\n",
    "$=\\frac{1}{\\sqrt{2}}(\\alpha|000\\rangle+\\alpha|011\\rangle+\\beta|100\\rangle+\\beta|111\\rangle)$. eq.(1.4.1)\n",
    "\n",
    "The CNOT gate is applied to the first two qubits with the first qubit being the control and the second qubit\n",
    "being the target:\n",
    "\n",
    "$CNOT|A^{'}\\rangle|\\psi^{+}\\rangle=\\frac{1}{\\sqrt{2}}(\\alpha|000\\rangle+\\alpha|011\\rangle+\\beta|110\\rangle+\\beta|101\\rangle)$. eq.(1.4.1)\n",
    "\n",
    "![Figure 1.4.3 Teleportation circuit 2](media/teleportation_circuit2.png)\n",
    "\n",
    "Figure 1.4.3 Alice applies CNOT using $|A^{'}\\rangle$ as control and her qubit in the Bell state as target.\n",
    "\n",
    "Next, Alice applies a H gate to $|A^{'}\\rangle$. The first qubit in eq. (1.4.1) changes accordingly:\n",
    "\n",
    "$\\frac{1}{\\sqrt{2}}[\\alpha(\\frac{|0\\rangle+|1\\rangle}{\\sqrt{2}})|00\\rangle+\\alpha(\\frac{|0\\rangle+|1\\rangle}{\\sqrt{2}})|11\\rangle+\\beta(\\frac{|0\\rangle-|1\\rangle}{\\sqrt{2}})|10\\rangle+\\beta(\\frac{|0\\rangle-|1\\rangle}{\\sqrt{2}})|01\\rangle]$\n",
    "$=\\frac{1}{2}[\\alpha(|0\\rangle+|1\\rangle)|00\\rangle+\\alpha(|0\\rangle+|1\\rangle)|11\\rangle+\\beta(|0\\rangle-|1\\rangle)|10\\rangle+\\beta(|0\\rangle-|1\\rangle)|01\\rangle]$\n",
    "$=\\frac{1}{2}[\\alpha(|000\\rangle+|100\\rangle)+\\alpha(|011\\rangle+|111\\rangle)+\\beta(|010\\rangle-|110\\rangle)+\\beta(|001\\rangle-|101\\rangle)]$\n",
    "$=\\frac{1}{2}[|00\\rangle(\\alpha|0\\rangle+\\beta|1\\rangle)+|01\\rangle(\\alpha\\rangle+\\beta|0\\rangle)+|10\\rangle(\\alpha|0\\rangle-\\beta|1\\rangle)+|11\\rangle(\\alpha|1\\rangle-\\beta|0\\rangle)]$. eq(1.4.2)\n",
    "\n",
    "The last step is taking out the first qubits Alice has and rearranging the equation into a format easier to\n",
    "use later. It looks complicated, but the math is very simple. Try to derive this yourself.\n",
    "\n",
    "![Figure 1.4.4 Teleportation circuit 3](media/teleportation_circuit3.png)\n",
    "\n",
    "Figure 1.4.4 After Alice entangles her Bell state qubit and the state she wishes to communicate.\n",
    "\n",
    "So far, all the information is contained in quantum states. Alice needs to tell Bob something about\n",
    "the interactions she did to the Bell state so that Bob can reproduce $|A^{'}\\rangle$. Some classical information needs\n",
    "to be shared. Alice does measurements on the two states in Figure 1.4.3. Remember she has the first two\n",
    "qubits and Bob has the last qubit. With two qubits, the outcome of measurements would be |00⟩, |01⟩,\n",
    "|10⟩ or |11⟩. If the first qubit is 0, the state in eq. (1.4.2) after measurement becomes\n",
    "\n",
    "$\\frac{1}{\\sqrt{2}}[|00\\rangle(\\alpha|1\\rangle+\\beta|1\\rangle)+|01\\rangle(\\alpha|1\\rangle+\\beta|0\\rangle)]$.\n",
    "\n",
    "If then another measurement is done on the second qubit and it is 0, the state becomes\n",
    "\n",
    "$\\frac{1}{2}[|00\\rangle(\\alpha|1\\rangle+\\beta|1\\rangle)]$.\n",
    "\n",
    "![Figure 1.4.5 Teleportation circuit 4](media/teleportation_circuit4.png)\n",
    "\n",
    "Figure 1.4.5 Measurements on the first and then the second qubits provide a list of possible results.\n",
    "\n",
    "This also tells us that the third qubit is in state $[\\alpha|0\\rangle+\\beta|1\\angle]$. Similarly, we can write out the possible\n",
    "states of the third qubit based on measurement results of the first three qubits:\n",
    "\n",
    "![Figure Teleportation Table](media/teleportation_table.png)\n",
    "\n",
    "Remember Bob has the third qubit and $\\alpha|0\\rangle+\\beta|1\\rangle$ is the state, $|A^{'}\\rangle$, Alice wanted Bob to get. Alice can\n",
    "call Bob and tell him her result is '00', and that Bob needs to do nothing. He already has |퐴퐴′⟩. If Alice\n",
    "measures '01', she can tell Bob that. Bob applies a X gate to $\\alpha|1\\rangle+\\beta|0\\rangle$, thus obtaining $|A^{'}\\rangle$. If Alice\n",
    "measures '10', Bob applies Z. If she measures '11', Bob applies X and Z. In other words, if the first qubit is\n",
    "1, Bob applies Z to his state; if the second qubit is 1, he applies X to his state. Therefore, the complete\n",
    "circuit diagram is constructed as shown in Figure 1.4.1. Teleportation of $|A^{'}\\rangle$ is achieved via communication\n",
    "of two bits of classical information.\n",
    "\n",
    "A common mistake people make when talking about entanglement is the misconception that\n",
    "there were instantaneous interactions between entangled states.\n",
    "\n",
    "![Figure 1.4.6 mistake](media/mistake.png)\n",
    "\n",
    "Alice and Bob know the state of one another's qubits, even without having to send a classical\n",
    "signal between themselves. Have they communicated any information? Could this process be used to\n",
    "violate the law that information can never travel faster than the speed of light? The answer to both\n",
    "questions is \"no\". When Alice measures her qubit in step 2, she randomly observes |0⟩ or |1⟩. Although\n",
    "she instantly knows the state of Bob's qubit, there is no way to use this knowledge to communicate a\n",
    "message to Bob. If Alice were somehow able to force nature to pick the state |0⟩|0⟩ or |1⟩|1⟩, then she\n",
    "could use this power to transmit a |0⟩ or |1⟩ to Bob. Bob could then look at his qubit and know which state\n",
    "Alice wanted to communicate to him. But Alice does not have this power – nature randomly chooses\n",
    "|0⟩|0⟩ or |1⟩|1⟩. Alice and Bob get the same random state, but they cannot use this to send a signal.\n",
    "\n",
    "![Figure 1.4.7 Faster than light](media/faster_than_light.png)\n",
    "\n",
    "That's not to say that knowing which random state the other person observed isn't useful. It can\n",
    "be very useful! Alice and Bob can use this procedure to generate a shared random encryption key. Alice\n",
    "can use the key to encrypt a message, which she can send to Bob over a classical channel. Bob can use the\n",
    "key to decrypt the message. They can't communicate faster than light, but at least they can communicate\n",
    "securely.\n",
    "\n",
    "### Q# exercise: Teleportation\n<a id='#qtransformation'></a>",
    "\n",
    "1. Go to QuantumComputingViaQSharpSolution introduced in session 1.1.\n",
    "2. Open 20 _Demo Teleportation Operation.qs in Visual Studio (Code).\n",
    "3. Lines 29-33 is setting up a sourceQubit, $|A^{'}\\rangle$, to be an arbitrary state. It has a 25% probability of\n",
    "    being |0⟩. Use Rx to make the qubit have the right angle on the Bloch sphere. You can change this\n",
    "    probability, hence the angle on the Bloch sphere, to anything you want later.\n",
    "\n",
    "![Figure 1.4.8 Exercise screen 1](media/teleportation_screen1.png)\n",
    "\n",
    "4. The lines that follow next are straightforward. The logics is a step-by-step code representation of\n",
    "    Figures 1.4.2 – 1.4.5.\n",
    "5. Run the script. The result \"countZero\" should be close to 250 and \"countOne\" close to 750,\n",
    "    indicating the 25% probability of being |0⟩. This is what we set $|A^{'}\\rangle$ to be at the beginning.\n",
    "6. Change to a different state and test the teleportation algorithm again.\n",
    "7. Go to the Quantum Katas: https://github.com/Microsoft/QuantumKatas.\n",
    "8. In Visual Studio (Code) open folder \"Teleportation\" and Task.qs. Or use the Jupyter Notebook\n",
    "    version as in the previous exercise. This exercise sets up the Teleportation circuit step by step.\n",
    "    Tasks 1.1-1.6 set up the operations. Later tasks are variations of the Teleportation setup.\n",
    "\n",
    "\n",
    "### Q# exercise: Another Teleportation Exercise\n<a id='#qtransformation2'></a>",
    "\n",
    "The Quantum Development Kit (QDK) has a sample test using Teleportation as the \"Hello World\" for Q#.\n",
    "\n",
    "1. Clone and open the Teleportation exercise\n",
    "\n",
    "git clone https://github.com/Microsoft/Quantum.git\n",
    "\n",
    "cd Quantum\n",
    "\n",
    "cd Samples/src/Teleportation/\n",
    "\n",
    "2. \"Here\" and \"there\" represent the two initial states. \"msg\" is the message qubit to be teleported.\n",
    "\n",
    "![Figure 1.4.9 excercise screen 2](media/teleportation_screen2.png)\n",
    "\n",
    "3. Lines 92-120 sets up the circuit so that\n",
    "```\n",
    "    /// If `true`, the source qubit (`here`) is prepared in the\n",
    "    /// |1〉 state, otherwise the source qubit is prepared in |0〉.\n",
    "    ///\n",
    "```\n",
    "4. Command \"dotnet run\" gives results\n",
    "```\n",
    "    Round 0: Sent True, got True.\n",
    "    Teleportation successful!!\n",
    "    Round 1: Sent False, got False.\n",
    "    Teleportation successful!!\n",
    "    ...\n",
    "    Round 6: Sent True, got True.\n",
    "    Teleportation successful!!\n",
    "    Round 7: Sent False, got False.\n",
    "    Teleportation successful!!\n",
    "```\n",
    "This setup only gives a \"True\" or \"False\" binary output. The TeleportationSample.qs in the\n",
    "QuantumComputingViaQSharpSolution exercise, however, allows us to use any message qubit.\n",
    "\n",
    "\n",
    "**Go to Quirk**\n",
    "\n",
    "Open [Quantum Teleportation](http://algassert.com/quirk#circuit={\"cols\":[[1,\"H\"],[1,\"•\",1,1,\"X\"],[\"…\",\"…\",1,1,\"…\"],[\"…\",\"…\",1,1,\"…\"],[\"~87lj\"],[\"Bloch\"],[\"•\",\"X\"],[\"H\"],[\"Measure\",\"Measure\"],[1,\"•\",1,1,\"X\"],[\"•\",1,1,1,\"Z\"],[1,1,1,1,\"Bloch\"],[1,1,1,1,\"~f7c0\"]],\"gates\":[{\"id\":\"~87lj\",\"name\":\"message\",\"circuit\":{) example. Watch how the Bloch spheres reflect the input and output\n",
    "qubits. The qubit retrieved matches exactly the message qubit.\n",
    "\n",
    "![Figure 1.4.8 Quirk teleportation](media/teleportation_quirk.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python38332bit225b1603132a4a1f9abff16033481403",
   "display_name": "Python 3.8.3 32-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}